package com.coolwen.controller;

import com.alibaba.fastjson.JSON;
import com.coolwen.entity.Resource;
import com.coolwen.entity.Role;
import com.coolwen.service.ResourceService;
import com.coolwen.service.RoleService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by user on .
 */
@Controller
@RequestMapping("/admin/role")
public class RoleController {
    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    private RoleService roleService;

    @Autowired
    private ResourceService resourceService;

    @RequestMapping("/list")
    public String list(Model model) {
        model.addAttribute("roles", roleService.listRole());
        return "role/list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        model.addAttribute("role", new Role());
        return "role/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Role role, Model model) {
        roleService.add(role);
        return "redirect:/admin/role/list";
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(Model model, @PathVariable int id) {
        model.addAttribute("role", roleService.load(id));
        return "role/update";
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public String update(@PathVariable int id, Role role, Model model) {
        Role rl = roleService.load(id);
        rl.setDescription(role.getDescription());
        rl.setRole(role.getRole());
        roleService.update(rl);
        return "redirect:/admin/role/list";
    }

    @RequestMapping("/listRes/{id}")
    public String listRes(Model model, @PathVariable int id) {
        logger.info("进入更新res" + JSON.toJSON(id));
        List<Resource> hasRes = roleService.listRoleResource(id);
        List<Long> hrIds = new ArrayList<Long>();
        for (Resource r : hasRes) {
            hrIds.add(r.getId());
        }
        model.addAttribute("rids", hrIds);
        model.addAttribute("reses", resourceService.listResource());
        model.addAttribute("role", roleService.load(id));
        return "role/res";
    }

    @RequestMapping(value = "/setRes", method = RequestMethod.POST)
    public void setRes(int resId, int roleId, int c, HttpServletResponse resp) throws IOException {
        logger.info("进入更新setRes,resId:"+resId+"roleID:"+roleId);
        if (c == 0) {
            logger.info("进入delsetRes,resId:"+resId+"roleID:"+roleId);
            roleService.deleteRoleResource(roleId, resId);
        } else {
            logger.info("进入addsetRes,resId:"+resId+"roleID:"+roleId);
            roleService.addRoleResource(roleId, resId);
        }
        resp.getWriter().println("ok");
    }
}
